package com.work.xxzl.module.main;


import android.content.Context;
import android.content.Intent;
import android.view.KeyEvent;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;

import com.work.xxzl.R;
import com.work.xxzl.adapter.MainPagerAdapter;
import com.work.xxzl.base.BaseActivity;
import com.work.xxzl.base.BaseFragmentForMain;
import com.work.xxzl.cache.Ckey;
import com.work.xxzl.cache.SPutils;
import com.work.xxzl.module.pagemine.LoginActivity;
import com.work.xxzl.utils.UIUtils;



/**
 * @author WZ 2016/12/3
 */
public class MainActivity extends BaseActivity {
    private static Context context;
    private RadioGroup main_radio;
    private FrameLayout content;
    private int index = 0;
    private MainPagerAdapter mMainPagerdapter;
    public static RadioButton rb_main_HomePage;
    private long exitTime = 0;


    @Override
    public void initView() {
        context = this;
        main_radio = (RadioGroup) findViewById(R.id.rg_main_radio);
        content = (FrameLayout) findViewById(R.id.fl_main_container);
        rb_main_HomePage = (RadioButton) findViewById(R.id.rb_main_HomePage);
        mMainPagerdapter = new MainPagerAdapter(getSupportFragmentManager());
        switchFragment(index);
        radioCheckedChange();
    }

    @Override
    protected void initData() {
        // 默认显示首页
        if (index == 0) {
            rb_main_HomePage.setChecked(true);
        }
        mTitleBar.titleBack.setVisibility(View.GONE);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_main;
    }

    private void radioCheckedChange() {

        main_radio.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    //首页
                    case R.id.rb_main_HomePage:
                        index = 0;
                        mTitleBar.titleMiddle.setText(R.string.onefragment);
                        break;
                    //通讯录
                    case R.id.rb_main_fenlei:
                        index = 1;
                        mTitleBar.titleMiddle.setText(R.string.secondfragment);
                        break;
                    //社圈
                    case R.id.rb_main_gwc:
                        index = 2;
                        mTitleBar.titleMiddle.setText(R.string.thirdfragment);
                        break;
//                    //
//                    case R.id.rb_main_dingdan:
//                        index = 3;
//                        mTitleBar.titleMiddle.setText("我的订单");
//                        break;
                    //我的
                    case R.id.rb_main_mine:
                        UIUtils.startActivity(LoginActivity.class);
                        index = 3;
                        mTitleBar.titleMiddle.setText(R.string.fivefragment);
                        break;
                }
                //显示碎片
                switchFragment(index);
            }

        });

    }

    /**
     * 显示碎片
     */
    private void switchFragment(int index) {
        mTitleBar.mBar.setVisibility(View.GONE);
        BaseFragmentForMain fragment = (BaseFragmentForMain) mMainPagerdapter
                .instantiateItem(content, index);
        mMainPagerdapter.setPrimaryItem(null, 0, fragment);
        mMainPagerdapter.finishUpdate(null);
    }


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
            exit();
            return false;
        }
        return super.onKeyDown(keyCode, event);
    }

    /***
     * 退出应用
     */
    public void exit() {
        if ((System.currentTimeMillis() - exitTime) > 2000) {
            UIUtils.showToast("再按一次退出应用");
            exitTime = System.currentTimeMillis();
            SPutils.remove(Ckey.USERID);
        } else {
            exitApp();
        }
    }

    /**
     * 退出应用
     */
    public void exitApp() {
        //关闭吐司
//        ToastUtils.cancel();
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_MAIN);
        intent.addCategory(Intent.CATEGORY_HOME);
        startActivity(intent);
    }

    public static Context getContext() {
        return context;
    }


}
